package com.lp.mapreduce;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 
 ************************************************************
 * @类名 : WebPvMapReduce.java
 *
 * @DESCRIPTION : web pv统计MapReduce 需求：统计每天每个省份的PV值
 * @AUTHOR : liaopin
 * @DATE : 2019年12月20日
 ************************************************************
 */
public class WebPvMapReduce extends Configured implements Tool {

    public static class WebPvMap extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
        private IntWritable outputKey = new IntWritable();
        private final IntWritable one = new IntWritable(1);

        @Override
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] lineValues = value.toString().split("\t");
            // URL为空不统计
            if (StringUtils.isEmpty(lineValues[1])) {
                context.getCounter("WebPvMapReduce_Counter", "URL_EMPTY").increment(1L);
                return;
            }
            // 规则，字段过少不统计
            if (lineValues.length < 30) {
                context.getCounter("WebPvMapReduce_Counter", "Length_limit_30").increment(1L);
                return;
            }
            String province = lineValues[23];
            if (StringUtils.isEmpty(province)) {
                context.getCounter("WebPvMapReduce_Counter", "proviceId_Empty").increment(1L);
                return;
            }
            try {
                int proviceId = Integer.valueOf(province);
                outputKey.set(proviceId);
                context.write(outputKey, one);
            } catch (Exception e) {
                context.getCounter("WebPvMapReduce_Counter", "proviceId_error").increment(1L);
                return;
            }
        }
    }

    public static class WebPvReduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
        private IntWritable outputValue = new IntWritable();

        @Override
        public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            outputValue.set(sum);
            context.write(key, outputValue);
        }

    }

    public int run(String[] args) throws Exception {
        Configuration conf = this.getConf();
        Job job = Job.getInstance(conf, WebPvMapReduce.class.getSimpleName());
        // 设置启动类
        job.setJarByClass(WebPvMapReduce.class);
        // input
        FileInputFormat.addInputPath(job, new Path(args[0]));
        // output
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // map
        job.setMapperClass(WebPvMap.class);
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(IntWritable.class);
        // reduce
        job.setReducerClass(WebPvReduce.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);
        // 提交job
        boolean isSuccess = job.waitForCompletion(true);
        return isSuccess ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        Configuration config = new Configuration();
        args = new String[] { "hdfs://bigdata-01.lp.com:8082/user/liaopin/input2", "hdfs://bigdata-01.lp.com:8082/user/liaopin/out14" };
        int status = ToolRunner.run(config, new WebPvMapReduce(), args);
        System.exit(status);
    }

}
